// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

/**
 * @brief    DTI Parser
 *
 * @version  0.1
 * @author   wuxingchuang
 * @date     2025.06.23
 * @copyright Copyright (c) 2025
 */

#include "Import/BDFImport/BulkData/Dynamic/SPSELParser.h"

#include <algorithm>
#include <cstdint>
#include <iostream>

#include "boost/algorithm/string.hpp"

#include "Utility/Logging/SGLogger.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Dynamic/ISPSELService.h"

#include "Import/BDFImport/BDFParserBase.h"

BULKDATA_ENTRY_PARSER_REG (SPSEL);

using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::DataStructure;

enum SPSELKey
{
    SPSELKEY_DTI   = 1,
    SPSELKEY_SPSEL = 2,
    SPSELKEY_RECNO = 3,
    SPSELKEY_DAMPL = 4,
    SPSELKEY_FREQL = 5,
};

SG::DataStructure::Common::Status BDF::BULKDATA::ParseSPSEL (const std::shared_ptr<SG::DBManager::DBServiceFactory>& dbServiceFactory,
                                                             std::vector<std::string>&                               dividedEntryInfo)
{
    int curKey = 1;

    SG::DataStructure::FEM::SPSELData tempData;

    bool hasTID = false;
    for (auto& curStrKey : dividedEntryInfo)
    {
        const bool isEmpty = is_all_space (curStrKey);
        if (isEmpty)
        {
            curKey++;
            continue;
        }

        boost::trim (curStrKey);
        switch (curKey)
        {
        case SPSELKEY_DTI:
            // 一般为"DTI"，可忽略或校验
            break;
        case SPSELKEY_SPSEL:
            break;
        case SPSELKEY_RECNO:
            tempData.m_id = convertToI64(curStrKey);
            break;
        case SPSELKEY_DAMPL:
            tempData.m_DAMPL = convertToI64 (curStrKey);
            break;
        case SPSELKEY_FREQL:
            tempData.m_FREQL = convertToI64 (curStrKey);
            break;
        default:
            tempData.m_Gi.push_back (convertToI64 (curStrKey));
            break;
        }
        curKey++;
    }

    // domainId可按需要设置
    tempData.m_domainId = 0;

    return dbServiceFactory->get<ISPSELService> ()->Add (tempData);
}